<template>
  <div style="margin-top: 20px">
    <el-row>
      <el-col :span="12">
        <div id="main" style="width: 100%; height: 450px"></div>
      </el-col>

      <el-col :span="12">
        <div id="pie" style="width: 100%; height: 400px"></div>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: "Home",
  data() {
    return {

    }
  },
  mounted() {  // 页面元素渲染之后再触发
    const option = {
      title: {
        text: '数量统计',
        subtext: '柱状图',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      xAxis: {
        type: 'category',
        data: []
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [],
          type: 'bar',
          itemStyle: {
            normal: {
              color: function (params) {
                const colorList = [
                  '#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B',
                  '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD',
                  '#D7504B', '#C6E579', '#F4E001', '#F0805A', '#26C0C0'
                ];
                return colorList[params.dataIndex]
              },
            }
          }
        },
      ]
    };

    // 饼图

    const pieOption = {
      title: {
        text: '数量统计',
        subtext: '比例图',
        left: 'center'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          type: 'pie',
          radius: '55%',
          center: ['50%', '70%'],
          data: [],  // 填空
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        },
      ],

    };


    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);

    var pieDom = document.getElementById('pie');
    var pieChart = echarts.init(pieDom);

    this.request.get("/echarts/data").then(res => {
      // 填空
      option.xAxis.data = res.data.map(v => v.name)
      option.series[0].data = res.data.map(v => v.value)
      // 数据准备完毕之后再set
      myChart.setOption(option);

      pieOption.series[0].data = res.data
      pieChart.setOption(pieOption)
    })
  }
}
</script>

<style scoped>

</style>
